package com.example.task03;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ResultActivity extends AppCompatActivity {

    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private Button button1;
    private double properWeight1 = 0.0;
    private String properWeight1_str = null;
    private String properWeight2_str = null;
    private double properWeight2 = 0.0;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_result);
        textView1 = findViewById(R.id.tv_range);
        textView2 = findViewById(R.id.tv_BMI);
        textView3 = findViewById(R.id.tv_summary);
        button1 = findViewById(R.id.btn_back);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ResultActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
        Intent intent = getIntent();
        if (intent != null) {
            double height = Double.parseDouble(intent.getStringExtra("height"))/100.0;//m为单位
            double weight = Double.parseDouble(intent.getStringExtra("weight"));
            double BMI = weight / Math.pow(height, 2);
            String BMI_str = String.format("%.2f", BMI);
            String gender = intent.getStringExtra("gender");
            if(gender.equals("女")){
                properWeight1 = 19.1 *Math.pow(height,2);
                properWeight1_str =String.format("%.2f", properWeight1);
                properWeight2 = 25.8 *Math.pow(height,2);
                properWeight2_str =String.format("%.2f", properWeight2);
                textView1.setText("您的合适体重范围为"+properWeight1_str+"-"+properWeight2_str+"kg");
                if (BMI < 17.5) {
                    textView2.setText("身体质量指数为"+ BMI_str +",属于厌食");
                    textView3.setText("请注意身体！");
                } else if(BMI<19.1){
                    textView2.setText("身体质量指数为"+ BMI_str +",体重偏轻");
                    textView3.setText("请好好吃饭！");
                }
                else if(BMI<25.8){
                    textView2.setText("身体质量指数为"+ BMI_str +",属于正常范围，");
                    textView3.setText("请继续保持！");
                }
                else if(BMI<27.3){
                    textView2.setText("身体质量指数为"+ BMI_str +",属于略微超重，");
                    textView3.setText("可以稍微少吃点！");
                }
                else{
                    textView2.setText("身体质量指数为"+ BMI_str +",属于超重，");
                    textView3.setText("该减肥啦");
                }
            }
            else if(gender.equals("男")){
                properWeight1 = 20.7 *Math.pow(height,2);
                properWeight1_str =String.format("%.2f", properWeight1);
                properWeight2 = 26.4 *Math.pow(height,2);
                properWeight2_str =String.format("%.2f", properWeight2);
                textView1.setText("您的合适体重范围为"+properWeight1_str+"-"+properWeight2_str+"kg");
                if (BMI < 17.5) {
                    textView2.setText("身体质量指数为"+ BMI_str +",属于厌食");
                    textView3.setText("请注意身体！");
                } else if(BMI<20.7){
                    textView2.setText("身体质量指数为"+ BMI_str +",体重偏轻");
                    textView3.setText("请好好吃饭！");
                }
                else if(BMI<26.4){
                    textView2.setText("身体质量指数为"+ BMI_str +",属于正常范围，");
                    textView3.setText("请继续保持！");
                }
                else if(BMI<27.8){
                    textView2.setText("身体质量指数为"+ BMI_str +",属于略微超重，");
                    textView3.setText("可以稍微少吃点！");
                }
                else{
                    textView2.setText("身体质量指数为"+ BMI_str +",属于超重，");
                    textView3.setText("该减肥啦");
                }
            }
            else{
                Toast.makeText(this, "性别未做选择，请选择！", Toast.LENGTH_SHORT).show();
            }
            super.onCreate(savedInstanceState);
        }
    }
}